from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serilizers import *
# from rest_framework.generics import GenericAPIView
# from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
# from rest_framework import generics
# from rest_framework.generics import CreateAPIView,RetrieveAPIView,DestroyAPIView,UpdateAPIView

# Create your views here.
#获取所有部门信息
# class DeptList(APIView):
#     #查询所有部门
#     def get(self,request):
#         deptlist = Dept.objects.all()
#         dept = DeptSer(deptlist,many=True)
#         return Response(dept.data)
#     #添加部门
#     def post(self,request):
#         ser = DeptSer(data=request.data)
#         if ser.is_valid():
#             ser.save()
#         else:
#             print(ser.errors)
#         return Response('ok')

# #GenericAPIView实现获取列表
# class DeptGen(GenericAPIView):
#     queryset = Dept.objects.all()
#     serializer_class = DeptSer
#     lookup_field = 'id'

#     #获取一条数据
#     def get(self, request,id):
#         book = self.get_object()
#         serializer = self.get_serializer(book)
#         return Response(serializer.data)

# class ListModelM(GenericAPIView,ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
#     queryset = Dept.objects.all()
#     serializer_class = DeptSer
#     lookup_field = 'id'

#     def get(self,request,id):
#         return self.retrieve(request)

#     def post(self,request):
#         return self.create(request)
    
#     def put(self,request,id):
#         return self.update(request)

#     def delete(self,request,id):
#         return self.destroy(request)

# class GenericList(UpdateAPIView):
#     queryset = Dept.objects.all()    
#     serializer_class = DeptSer
#     lookup_field='id'

# class CreateDept(CreateAPIView):
#     serializer_class = DeptSer


#添加获取
class DeptManage(APIView):
    #添加数据
    def post(self,reqeuest):
        mes = {}
        #获取用户输入
        data1 = reqeuest.data
        #调用序列化器进行
        ser = DeptSer(data = data1)
        if ser.is_valid():
            ser.save()
            mes['code'] = 200
            mes['message'] = '成功'
        else:
            print(ser.errors)
            mes['code'] = 10010
            mes['message'] = '失败'
        #添加成功返回结果
        return Response('ok')
    
    #获取所有部门信息
    def get(self,reqeuest):
        #获取所有部门信息
        dept = Dept.objects.all()
        #调用序列化器
        ser = DeptSer(dept,many=True)
        #返回结果
        return Response({'code':200,'dept':ser.data})

from rest_framework.generics import GenericAPIView

class DeptGenManage(GenericAPIView):
    #设置查询集
    queryset = Dept.objects.all()
    #设置序列化器
    serializer_class = DeptSer

    lookup_field = 'id'

    # #获取所有部门信息
    # def get(self,request):
    #     #获取查询集
    #     dept = self.get_queryset()
    #     #获取序列化对象
    #     ser = self.get_serializer(dept,many=True)
    #     return Response(ser.data)
    #获取详细
    def get(self,request,id):
        
        dept = self.get_object()
        ser = self.get_serializer(dept)
        
        return Response(ser.data)
        return Response('ok')

from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
#mixin实现添加修改删除
class DeptMixinMange(GenericAPIView,ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin):
    #返回所有数据
    queryset = Dept.objects.all()
    serializer_class = DeptSer
    lookup_field='id'
    #获取所有数据
    def get(self,request):
        return self.list(request)
    #添加数据
    def post(self,request):
        return self.create(request)
    #更新数据
    def put(self,request,id):
        return self.update(request)
    #删除数据
    def delete(self,request,id):
        return self.destroy(request)

#能用视图
from rest_framework import generics

class DeptGeneric(generics.ListAPIView):
    queryset = Dept.objects.all()
    serializer_class = DeptSer
